package cn.hebutrun.oulabot.common.util;

import cn.hebutrun.oulabot.common.Constants;
import cn.hebutrun.oulabot.common.UrlEnum;
import cn.hebutrun.oulabot.entity.request.MessageRequest;
import cn.hebutrun.oulabot.entity.response.BotFriendListResponse;
import cn.hebutrun.oulabot.entity.response.BotMessageResponse;
import cn.hebutrun.oulabot.entity.response.GroupMemberInfoResponse;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.Map;

/**
 * @author zhaolong
 * @since 2022-09-29
 */
@UtilityClass
public class BotUtil {

  public void sendMessage(String messageType, Long groupId, Long userId, String msg) {
    HttpUtil.post(
        Constants.BOT_BASE_URL + UrlEnum.SEND_MESSAGE_URL.getUrl(),
        JSONUtil.toJsonStr(
            new MessageRequest()
                .setMessage_type(messageType)
                .setGroup_id(groupId)
                .setUser_id(userId)
                .setMessage(msg)));
  }

  public void sendMessage(BotMessageResponse botMessageResponse, String msg) {
    BotUtil.sendMessage(
        botMessageResponse.getMessageType(),
        botMessageResponse.getGroupId(),
        botMessageResponse.getUserId(),
        msg);
  }

  /**
   * 标记消息已读
   *
   * @param messageId 消息id
   * @since 2022/10/25
   * @author zhaolong
   */
  public void markMsgAsRead(long messageId) {
    HttpUtil.get(
        Constants.BOT_BASE_URL + UrlEnum.MARK_MSG_AS_READ_URL.getUrl(),
        Map.of("message_id", messageId));
  }

  public String getCQAtStr(Long qq) {
    return StrUtil.format(Constants.AT_EXPRESSION, qq);
  }

  public String getCQAtStr(String qq) {
    return StrUtil.format(Constants.AT_EXPRESSION, qq);
  }

  public String getCQAtAll() {
    return StrUtil.format(Constants.AT_EXPRESSION, "all");
  }

  public String getCQImgStr(String imgUrl) {
    return StrUtil.format(Constants.IMG_EXPRESSION, imgUrl);
  }

  /**
   * 获取回复消息CQ表达式
   *
   * @param messageId 消息id
   * @param repliedUserId 被回复人(消息id匹配的消息发送人)
   * @return java.lang.String
   * @since 2022/10/24
   * @author zhaolong
   */
  public String getCQReply(String messageId, String repliedUserId) {
    return StrUtil.format(Constants.REPLY_EXPRESSION, messageId, repliedUserId);
  }

  public List<BotFriendListResponse.FriendListData> getFriendList() {
    String resJsonStr = HttpUtil.get(Constants.BOT_BASE_URL + "/get_friend_list");
    BotFriendListResponse botFriendListResponse =
        JSONUtil.toBean(resJsonStr, BotFriendListResponse.class);
    return botFriendListResponse.getData();
  }

  public List<String> getFriendQQList() {
    return getFriendList().stream()
        .map(friendListData -> String.valueOf(friendListData.getUser_id()))
        .toList();
  }

  /**
   * 处理添加好友
   *
   * @param flag 加好友请求的 flag（需从上报的数据中获得）
   * @since 2022/10/11
   * @author zhaolong
   */
  public void addFriend(String flag) {
    addFriend(flag, true, "");
  }

  /**
   * 处理添加好友
   *
   * @param flag 加好友请求的 flag（需从上报的数据中获得）
   * @param approve 是否同意请求
   * @param remark 添加后的好友备注（仅在同意时有效）
   * @since 2022/10/11
   * @author zhaolong
   */
  private void addFriend(String flag, boolean approve, String remark) {
    HttpUtil.get(
        Constants.BOT_BASE_URL + "/set_friend_add_request",
        Map.of("flag", flag, "approve", approve, "remark", remark));
  }

  public void sendReadme(BotMessageResponse botMessageResponse) {
    BotUtil.sendMessage(botMessageResponse, StrUtil.format(Constants.IMG_EXPRESSION, "README.png"));
  }

  /**
   * 判断群中是否有该qq成员
   *
   * @param groupId 群聊id
   * @param qq 被判断人qq号
   * @return boolean
   * @since 2022/11/17
   * @author zhaolong
   */
  public boolean isGroupMember(Long groupId, Long qq) {
    String resStr =
        HttpUtil.get(
            Constants.BOT_BASE_URL + "/get_group_member_list", Map.of("group_id", groupId));
    return JSONUtil.toBean(resStr, GroupMemberInfoResponse.class).getData().stream()
        .anyMatch(x -> x.getUser_id().equals(qq));
  }
}
